package org.hgm.controller;

import java.net.URL;
import java.util.Iterator;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

@Controller
public class RSSController {

	@RequestMapping(value = "/rss.htm", method = RequestMethod.GET)
	public String getRSSFeeds(Model model) throws Exception {
		URL url = new URL("http://aniruddhafriend-samirsinh.com/feed/");
		XmlReader reader = null;

		try {

			reader = new XmlReader(url);
			SyndFeed feed = new SyndFeedInput().build(reader);
			System.out.println("Feed Title: " + feed.getAuthor());
			model.addAttribute("syndEntrys", feed.getEntries());
			for (Iterator i = feed.getEntries().iterator(); i.hasNext();) {
				SyndEntry entry = (SyndEntry) i.next();
				System.out.println(entry.getTitle());
				System.out.println(entry.getDescription());
				System.out.println(entry.getAuthor());
			}
		} finally {
			if (reader != null)
				reader.close();
		}

		return "rss";
	}
}
